/*
 * @Author: lvye
 * @Description:
 * @Date: 2023-08-22 15:41:39
 * @LastEditTime: 2023-09-07 16:06:43
 * @FilePath: \vue3-ts\src\store\system\system.ts
 */
import { Module } from "vuex"
import { IRootState } from "@/store/type"
import { ISystemState } from "./type"
import {
  getPageListData,
  createPageData,
  editPageData
} from "@/service/main/system/system"
import tableData from "@/mock/table"
const systemMoudel: Module<ISystemState, IRootState> = {
  namespaced: true,
  state() {
    return {
      usersList: [],
      usersCount: 0,
      roleList: [],
      roleCount: 0
    }
  },
  mutations: {
    changeUsersList(state, userList: any[]) {
      state.usersList = userList
    },
    changeUsersCount(state, usersCount: number) {
      state.usersCount = usersCount
    },
    changeRoleList(state, list: any[]) {
      state.roleList = list
    },
    changeRoleCount(state, count: number) {
      state.roleCount = count
    }
  },
  getters: {
    pageListData(state) {
      return (pageName: string) => {
        return (state as any)[`${pageName}List`]
      }
    },
    pageListCount(state) {
      return (pageName: string) => {
        return (state as any)[`${pageName}Count`]
      }
    }
  },
  actions: {
    //获取用户列表
    async getPageListAction({ commit }, payload: any) {
      const pageName = payload.pageName
      const pageUrl = `/${pageName}/list`
      const pageResult = await getPageListData(pageUrl, payload.queryInfo)
      console.log(pageResult, "ccc")
      // 接口不通 mock数据
      const totalCount = 20
      const changePageName =
        pageName.slice(0, 1).toUpperCase() + pageName.slice(1)
      console.log(changePageName, "changePageName")
      console.log(pageName, "pageName")
      commit(`change${changePageName}List`, tableData)
      commit(`change${changePageName}Count`, totalCount)
    },
    // 创建列表
    async createPageDataAction({ dispatch }, payload: any) {
      const { pageName, newData } = payload
      const pageUrl = `/${pageName}`
      const pageResult = await createPageData(pageUrl, newData)
      console.log(pageResult, "ccc")
      // 重新获取列表数据
      dispatch("getPageListAction", {
        pageName,
        queryInfo: {
          offset: 0,
          size: 10
        }
      })
    },
    // 编辑列表
    async editPageDataAction({ dispatch }, payload: any) {
      const { pageName, editData, id } = payload
      const pageUrl = `/${pageName}/${id}`
      const pageResult = await editPageData(pageUrl, editData)
      console.log(pageResult, "ccc")
      // 重新获取列表数据
      dispatch("getPageListAction", {
        pageName,
        queryInfo: {
          offset: 0,
          size: 10
        }
      })
    }
  }
}
export default systemMoudel
